#include <my_header.h>

//处理“ls”函数
int client_ls(int net_fd, const char* order){
    char new_order[1024] = {0};
    strcpy(new_order, order);
    strcat(new_order, " > order.txt");
    int fd = open("order.txt", O_RDWR | O_CREAT, 0666); 
    char buf[1024] = { 0 };
    bzero(buf, sizeof(buf));
    lseek(fd, 0, SEEK_SET);  // 重新定位到文件开始
    read(fd, buf, sizeof(buf));
    system(new_order);
    send(net_fd, buf, sizeof(buf), MSG_NOSIGNAL);
    system("rm -r order.txt");
    return 0;
}

//处理pwd命令
int client_pwd(int net_fd, const char* order){
    char new_order[1024] = {0};
    strcpy(new_order, order);
    strcat(new_order, " > order.txt");
    int fd = open("order.txt", O_RDWR | O_CREAT, 0666); 
    char buf[1024] = { 0 };
    bzero(buf, sizeof(buf));
    lseek(fd, 0, SEEK_SET);  // 重新定位到文件开始
    read(fd, buf, sizeof(buf));

    //clients是用户目录
    strtok(buf, "\clients");
    char *pwd = strtok(NULL, "\0");
    system(new_order);
    send(net_fd, pwd, 1024, MSG_NOSIGNAL);
    system("rm -r order.txt");
    return 0;
}

//处理mkdir命令
int client_mkdir(int net_fd, const char* order){
    char new_order[1024] = {0};
    strcpy(new_order, "mkdir ");
    strcat(new_order, order);
  
      
    char buf[1024];
    memset(buf, 0, sizeof(buf));
    int res = system(new_order);
    if(res != 0){
        strcpy(buf, "目录已存在！");
        send(net_fd, buf, strlen(buf), MSG_NOSIGNAL);
    }else{
        strcpy(buf, "目录创建成功！");
        send(net_fd, buf, strlen(buf), MSG_NOSIGNAL);
    }
    return 0;
}

